package com.demo.petshop.controller;

import com.demo.petshop.entity.Orders;
import com.demo.petshop.entity.Products;
import com.demo.petshop.service.OrdersService;
import com.demo.petshop.util.Result;
import com.mybatisflex.core.paginate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 *  控制层。
 *
 * @author lenovo
 * @since 2025-06-11
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @PostMapping("save")
    public boolean save(@RequestBody Orders orders) {
        return ordersService.save(orders);
    }

    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return ordersService.removeById(id);
    }

    @DeleteMapping("delete")
    public Result delete(@RequestParam Integer orderId){
            return Result.success(ordersService.deleteById(orderId));
    }

    @PutMapping("update")
    public Object update(@RequestBody Orders orders) {
        return Result.success(ordersService.updateById(orders));
    }

    @GetMapping("list")
    public List<Orders> list() {
        return ordersService.list();
    }

    @GetMapping("getInfo")
    public Result getInfo(@RequestParam Integer orderId) {
        return Result.success(ordersService.getInfo(orderId));
    }


    @GetMapping("page")
    public Page<Orders> page(Page<Orders> page) {
        return ordersService.page(page);
    }

    @GetMapping("count")
    public Result count(){
        return Result.success(ordersService.count());
    }
    @GetMapping("getByStatus")
    public Result getByStatus(@RequestParam String status){
        return Result.success(ordersService.getByStatus(status));
    }

    @GetMapping("confirm")
    public Result confirm(@RequestParam Integer orderId){
        return Result.success(ordersService.confirm(orderId));
    }



    @PostMapping("createOrder")
    public Result createOrder(@RequestBody Orders order){
        return Result.success(ordersService.createOrder(order));
    }

    @PostMapping("repeatPurchase")
    public Object repeatPurchase(@RequestBody List<Products> productsList){
        return ordersService.repeatPurchase(productsList);
    }

    /**
     * 按商店批量创建订单
     *
     * @param ordersList 按商店分组的订单列表
     * @return 创建结果
     */
    @PostMapping("createBatchOrders")
    public Result createBatchOrders(@RequestBody List<Orders> ordersList){
        return Result.success(ordersService.createBatchOrders(ordersList));
    }

    @GetMapping("cancel")
    public Result cancel(@RequestParam Integer orderId){
        return Result.success(ordersService.cancel(orderId));
    }
    @GetMapping("counts")
    public Object counts(){
        return ordersService.counts();
    }

    @PostMapping("paymentOrder")
    public Result paymentOrder(@RequestBody Orders orders){
        return Result.success(ordersService.paymentOrder(orders));
    }
}
